package com.kotlin.hxh.ui.fragment.third.child.child

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.kotlin.hxh.R
import com.kotlin.hxh.adapter.MenuAdapter
import com.kotlin.hxh.base.BaseMainFragment
import com.kotlin.hxh.listener.OnItemClickListener
import com.kotlin.hxh.ui.fragment.third.child.ShopFragment
import kotlinx.android.synthetic.main.fragment_list_menu.*


import java.util.*

/**
 * @author huxh
 * @date 2018/12/20.
 */
class MenuListFragment : BaseMainFragment() {
    private val mAdapter: MenuAdapter by lazy { MenuAdapter() }

    private var mMenus: ArrayList<String>? = null
    private var mCurrentPosition = -1

    companion object {
        private val ARG_MENUS = "arg_menus"
        private val SAVE_STATE_POSITION = "save_state_position"
        @JvmStatic
        fun newInstance(menus: ArrayList<String>): MenuListFragment {
            val args = Bundle()
            args.putStringArrayList(ARG_MENUS, menus)
            val fragment = MenuListFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mMenus = arguments?.getStringArrayList(ARG_MENUS)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) = inflater.inflate(R.layout.fragment_list_menu, container, false)

    override fun onCreateFragmentAnimator() = DefaultNoAnimator()


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val manager = androidx.recyclerview.widget.LinearLayoutManager(_mActivity)
        recy.setLayoutManager(manager)
        recy.setAdapter(mAdapter)
        mAdapter.setDatas(mMenus!!)

        mAdapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(position: Int, view: View?, vh: androidx.recyclerview.widget.RecyclerView.ViewHolder) {
                showContent(position)
            }
        })

        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(SAVE_STATE_POSITION)
            mAdapter.setItemChecked(mCurrentPosition)
        } else {
            mCurrentPosition = 0
            mAdapter.setItemChecked(0)
        }
    }

    private fun showContent(position: Int) {
        if (position == -1 || position == mCurrentPosition) {
            return
        }

        mCurrentPosition = position

        mAdapter.setItemChecked(position)

        val fragment = ContentFragment.newInstance(mMenus!![position])

        (parentFragment as ShopFragment).switchContentFragment(fragment)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt(SAVE_STATE_POSITION, mCurrentPosition)
    }
}